package com.example.sendwebsocket.config;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

public class GlobalConfig {
    public static  WebSocketClient webSocketClient(String url) {
    try {                                   //新建一个webSocketClient对象
            WebSocketClient webSocketClient = new WebSocketClient(new URI(url)) {//new Draft_6455()协议版本
                @Override
                public void onOpen(ServerHandshake serverHandshake) {

                }

                @Override
                public void onMessage(String s) {

                }

                @Override
                public void onClose(int i, String s, boolean b) {

                }

                @Override
                public void onError(Exception e) {

                }
            };
            //初始化websocket连接。 会阻塞 返回成功或者失败。
        boolean b = webSocketClient.connectBlocking();
        System.out.println(b);
        return webSocketClient;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
